package cc.mrbird.febs.power.controller;


import cc.mrbird.febs.common.utils.R;
import cc.mrbird.febs.power.entity.RoleBindAppInfo;
import cc.mrbird.febs.power.entity.RoleBindMenuInfo;
import cc.mrbird.febs.power.entity.RoleInfo;
import cc.mrbird.febs.power.service.IRoleBindAppInfoService;
import cc.mrbird.febs.power.service.IRoleBindMenuInfoService;
import cc.mrbird.febs.power.service.IRoleInfoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MrBird
 */
@RestController
@RequestMapping("/power/role-info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleInfoController {

    private final IRoleInfoService iRoleInfoService;

    private final IRoleBindAppInfoService iRoleBindAppInfoService;

    private final IRoleBindMenuInfoService iRoleBindMenuInfoService;

    private SimpleDateFormat sdf3=new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 分页获取角色信息
     * @param page
     * @param roleInfo
     * @return
     */
    @GetMapping("/page")
    public R getRoleInfoByPage(Page page,RoleInfo roleInfo) {
        return R.ok(iRoleInfoService.page(page,Wrappers.<RoleInfo>lambdaQuery().like(roleInfo.getRoleName()!=null,RoleInfo::getRoleName,roleInfo.getRoleName())));
    }

    /**
     * 获取所有角色信息
     * @return
     */
    @GetMapping
    public R getRoleInfo() {
        return R.ok(iRoleInfoService.list());
    }

    /**
     * 新增角色信息
     * @param roleInfo
     * @return
     */
    @PostMapping("/RoleInfoAdd")
    public R RoleInfoAdd(RoleInfo roleInfo,String appId,String menuId) {
        roleInfo.setCreateDate(sdf3.format(new Date()));
        iRoleInfoService.save(roleInfo);
        if(appId!=null){
            iRoleBindAppInfoService.save(new RoleBindAppInfo(roleInfo.getRoleId(),appId));
        }
        if(menuId!=null){
            iRoleBindMenuInfoService.save(new RoleBindMenuInfo(roleInfo.getRoleId(),menuId));
        }
        return R.ok(roleInfo.getRoleId());
    }

    /**
     * 根据角色id获取详细信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R getMessageByRole(@PathVariable("id") Integer id) {
        RoleBindMenuInfo bindMenuInfo = iRoleBindMenuInfoService.getOne(Wrappers.<RoleBindMenuInfo>lambdaQuery().eq(RoleBindMenuInfo::getRoleId,id));
        RoleBindAppInfo bindAppInfo = iRoleBindAppInfoService.getOne(Wrappers.<RoleBindAppInfo>lambdaQuery().eq(RoleBindAppInfo::getRoleId,id));
        Map map = new HashMap();
        if(bindMenuInfo!=null){
            map.put("bindMenuInfo",bindMenuInfo.getMenuId().split(","));
        }
        if(bindAppInfo!=null){
            map.put("bindAppInfo",bindAppInfo.getAppId().split(","));
        }
        return R.ok(map);
    }

    /**
     * 修改角色信息
     * @param roleInfo
     * @return
     */
    @PutMapping("/RoleInfoEdit")
    public R RoleInfoEdit(RoleInfo roleInfo,String appId,String menuId) {
        roleInfo.setUpdateDate(sdf3.format(new Date()));
        if(appId!=null){
            iRoleBindAppInfoService.update(Wrappers.<RoleBindAppInfo>lambdaUpdate().set(RoleBindAppInfo::getAppId,appId).eq(RoleBindAppInfo::getRoleId,roleInfo.getRoleId()));
        }
        if(menuId!=null){
            iRoleBindMenuInfoService.update(Wrappers.<RoleBindMenuInfo>lambdaUpdate().set(RoleBindMenuInfo::getMenuId,menuId).eq(RoleBindMenuInfo::getRoleId,roleInfo.getRoleId()));
        }
        return R.ok(iRoleInfoService.updateById(roleInfo));
    }

    /**
     * 删除记录
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R RoleInfoDel(@PathVariable Integer id) {
        return R.ok(iRoleInfoService.removeById(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/man/{ids}")
    public R RoleInfosDel(@PathVariable("ids") List<Integer> ids) {
        return R.ok(iRoleInfoService.removeByIds(ids));
    }

}
